package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.CoachInterpretationModel;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.WebViewCssUtil;
import com.banshuibao.bsb_android.util.WebViewUtil;

/**
 * Created by zyh on 2018/5/10.
 */

public class CoachPolicyAdapter extends RecyclerView.Adapter<CoachPolicyAdapter.ViewHolder> {

    private Context context;

    private CoachInterpretationModel.Data data;

    private String title;

    private long time;

    public CoachPolicyAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_coach_policy_detail, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder.webView != null) {
            holder.time.setText(TimeUtil.format(time));
            holder.title.setText(title);
            if (data != null) {
                String newHtml = WebViewCssUtil.setCss(data.getContent());
                holder.webView.loadDataWithBaseURL(null, newHtml, "text/html", "utf-8", null);
            }
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setData(CoachInterpretationModel.Data data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setTime(long time) {
        this.time = time;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView title, time;

        private WebView webView;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title_text_);
            title.getPaint().setFakeBoldText(true);
            time = itemView.findViewById(R.id.time);
            webView = itemView.findViewById(R.id.webView);
            WebViewUtil.addImageClickListener(context, webView);
        }
    }

}
